package com.jiyun.h2303b.utils;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jiyun.h2303b.config.AlipayConfig;
import com.jiyun.h2303b.pojo.AlipayBean;
import org.springframework.stereotype.Component;

@Component
public class AlipayUtils {

    /**
     * 支付宝支付
     * @param alipayBean
     * @return
     * @throws Exception
     */
    public static String pay(AlipayBean alipayBean) throws Exception{
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ alipayBean.getOut_trade_no() +"\","
                + "\"total_amount\":\""+ alipayBean.getTotal_amount() +"\","
                + "\"subject\":\""+ alipayBean.getSubject() +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        return result;
    }
}
